package com.et.mclient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MClient {

	public static void main(final String[] args) throws IOException {

		System.out.println("start test");

		final String host = "127.0.0.1";
		final int port = 9998;

		final Socket socket = new Socket(host, port);
		socket.setTcpNoDelay(true);
		socket.setReceiveBufferSize(16 * 1024);
		socket.setSendBufferSize(16 * 1024);



		final DataOutputStream out = new DataOutputStream(socket.getOutputStream());
		final DataInputStream in = new DataInputStream(socket.getInputStream());


		try {

			// step1: send a xml message
			final String m1 = "<verify>12";
			final String m3 = "</verify>";
			final byte[] b1 = m1.getBytes("UTF-8");
			final byte[] b3 = m3.getBytes("UTF-8");
			System.out.println(b1.length);
			System.out.println(b3.length);

			out.writeInt(b1.length + b3.length);
			out.write(b1);

			Thread.sleep(5000);

			out.write(b3);

			int len = in.readInt();

			if (len > 0) {
				final byte[] receiveBuffer = new byte[len];
				in.read(receiveBuffer);

				final String re = new String(receiveBuffer);

				System.out.println(re);
			}

			// step2: send a binary message
			final String m2 = "<verify><user>n@corpmail</user><sid>abcdefghijklmnopqrstuvwxyz01234567890</sid><cookie>eJ</cookie></verify>";
			final byte[] b2 = m2.getBytes("UTF-8");
			System.out.println(b2.length);

			out.writeInt(b2.length);
			out.write(b2);

			len = in.readInt();
			if (len > 0) {
				final byte[] receiveBuffer = new byte[len];
				in.read(receiveBuffer);

				final String re = new String(receiveBuffer);

				System.out.println(re);
			}

		} catch (Exception e) {
			System.out.print(e.getMessage());
		}

		socket.close();
	}
}
